﻿using System;
using System.Windows;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;

namespace Baosight.ZNH.ZMCE.UI.PopWindows
{
    public partial class FaultPhenomenon : EFViewCmes
    {
        public FaultPhenomenon(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
            this.LayoutUpdated+=new EventHandler(FaultPhenomenon_LayoutUpdated);
        }

        /// <summary>
        /// 过滤指定类型的标准设备
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FaultPhenomenon_LayoutUpdated(object sender, EventArgs e)
        {
            equipment.eViewInitParams = "bindingService=ZM06.queryStandardEquipment,columns=fdCode|fdName|fdSpecifications|fdModel,queryConditions=fdName,initParam=fdToencode:" + this.fdClassToencode.Value;
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.spWindows.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }

        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            base.OnCommitButtonClick(sender, e);
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 带出标准设备信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdEquipmentId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue == null && e.OldValue.Equals(""))
            {
                return;
            }

            EFDataField field = sender as EFDataField;
            CmesCustomView picker = field.Content as CmesCustomView;

            if (picker.ItemObject != null)
            {
                if (!picker.ItemObject.Equals(""))
                {
                    this.fdToencode.Value = picker.ItemObject.GetType().GetProperty("fdToencode").GetValue(picker.ItemObject, null) as string;
                    this.fdEquipmentSpecifications.Value = picker.ItemObject.GetType().GetProperty("fdSpecifications").GetValue(picker.ItemObject, null) as string;
                    this.fdEquipmentModel.Value = picker.ItemObject.GetType().GetProperty("fdModel").GetValue(picker.ItemObject, null) as string;
                }
                //没选中时赋予空字符串
                else
                {
                    this.fdToencode.Value = "";
                    this.fdEquipmentSpecifications.Value = "";
                    this.fdEquipmentModel.Value = "";
                }
            }
        }

    }
}
